package Schedule::Chronic::Distributed::Constraint;

=head1 NAME

Schedule::Chronic::Distributed::Constraint - Scheduler constraints base class

=head1 SYNOPSIS

use base qw(Schedule::Chronic::Distributed::Constraint);

=head1 DESCRIPTION

Base class for system constraints.

System constraints (as opposed to task requirements) allow you to constrain
when tasks are run by the system resources of the node.

The resources measured are Memory, Disk IO, Load Average and Swap Memory, you can
create your own based on these pretty trivially.

The values returned by each system constraint are used when querying the database
for which task a node should run next, as any task can have constraints such as
'Load Average below X', and any task not meeting those requirements will not be
run on the node, unless it is overdue.

=head1 METHODS

=head2 ACCESSORS

=over

=item type

=item id

=back

=head2 initialise_class

Class method to prepare a constraint by populating based on information from
the appropriate Schedule::Chronic::Distributed::Requirement object in the database.

=head2 new

Constructor to instantiate and return a new object.

=head2 update_last_time

Updates internal timers

=cut

use strict;

use DateTime;
use Schedule::Chronic::Distributed::Requirement;

use base qw(Class::Data::Inheritable);

__PACKAGE__->mk_classdata('type');
__PACKAGE__->mk_classdata('id');

use constant _MIN_REFRESH => 0;
use constant _STATE => 1;
use constant _LAST_TIME => 2;

sub initialise_class {
    my $class = shift;
    (my $requirement_name = $class) =~ s/Schedule::Chronic::Distributed::Constraint:://;
    my ($requirement) = Schedule::Chronic::Distributed::Requirement->search(handler => $requirement_name);
    $class->type($requirement->type);
    $class->id($requirement->id);
}

sub new {
    my $class = shift;
    my $now = DateTime->now;
    my $self = bless ([ 15, 0.00, $now->date . ' ' . $now->time ], $class);
    return $self;
}

sub update_last_time {
    my $now = DateTime->now;
    shift()->[_LAST_TIME] = $now->date . ' ' . $now->time;
}

1;


__END__

=head1 SEE ALSO

Schedule::Chronic::Distributed

=head1 AUTHOR

aaron trevena, E<lt>aaron.trevena@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Aaron Trevena

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.

=cut
